﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace MacroprocesosWinForms.Usuario
{
    public partial class WordViewer : UserControl
    {
        delegate void ConvertDocumentDelegate(string filename);
        string tempFileName = null;

        public WordViewer()
        {
            InitializeComponent();
        }

        public void LoadDocument(string fileName)
        {
            //Call ConvertDocument Asynchronously
            ConvertDocumentDelegate del = new ConvertDocumentDelegate(ConvertDocument);

            del.BeginInvoke(fileName, DocumentConversionComplete, null);
        }

        void ConvertDocument(string fileName)
        {
            object m = System.Reflection.Missing.Value;
            object oldFileName = (object)fileName;
            object readOnly = (object)false;
            Microsoft.Office.Interop.Word.Application ac = null;

            try
            {
                ac = new Microsoft.Office.Interop.Word.Application();

                Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                    ref m, ref m, ref m, ref m, ref m, ref m);

                //Create a temp File to save the HTML file to.

                tempFileName = GetTempFile("html");

                //Cast these items to object. The methods we're calling
                //only take object types in their methos parameters
                object newFileName = (object)tempFileName;

                // We will be saving this file as HTML format. 
                object fileType = (object)WdSaveFormat.wdFormatHTML;

                //Save the File
                doc.SaveAs2(ref newFileName, ref fileType,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            }
            finally
            {
                //Make sure we close the application class
                if (ac != null)
                    ac.Quit(ref readOnly, ref m, ref m);
            }
        }

        void DocumentConversionComplete(IAsyncResult result)
        {
            //navigate to pur temp file
            webBrowserPrincipal.Navigate(tempFileName);
        }

        private void webBrowserPrincipal_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (tempFileName != string.Empty)
            {
                //delete the temp file we created
                File.Delete(tempFileName);

                //set the tempFile to an empty string
                tempFileName = string.Empty;
            }
        }

        string GetTempFile(string extension)
        {
            // Uses the Combine, GetTempPath, ChangeExtension, 
            // and GetRandomFile methods of Path to 
            // create a temp file of the extension we're looking for. 
            return Path.Combine(Path.GetTempPath(),
                Path.ChangeExtension(Path.GetRandomFileName(), extension));
        }
    }
}
